import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards } from '@nestjs/common';
import { RoleService } from './role.service';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
import { RolesGuard } from '../../guards/roles.guard';
import { PermissionsGuard } from '../../guards/permissions.guard';
import { Roles } from '../../decorators/roles.decorator';
import { Permissions } from '../../decorators/permissions.decorator';
import { Public } from '../../decorators/public.decorator';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { AssignPermissionsDto } from './dto/assign-permissions.dto';
import { AssignRolesDto } from './dto/assign-roles.dto';

@ApiTags('角色管理')
@Controller('roles')
@UseGuards(JwtAuthGuard, RolesGuard, PermissionsGuard)
@ApiBearerAuth()
export class RoleController {
  constructor(private roleService: RoleService) {}

  @Post()
  @Roles('系统管理员')
  @Permissions('system:role:create')
  @ApiOperation({ summary: '创建角色' })
  @ApiResponse({ status: 201, description: '角色创建成功' })
  async createRole(@Body() createRoleDto: CreateRoleDto) {
    return this.roleService.createRole(
      createRoleDto.name,
      createRoleDto.code,
      createRoleDto.description,
      createRoleDto.type
    );
  }

  @Put(':id')
  @Roles('系统管理员')
  @Permissions('system:role:update')
  @ApiOperation({ summary: '更新角色' })
  @ApiResponse({ status: 200, description: '角色更新成功' })
  async updateRole(
    @Param('id') id: string,
    @Body() updateRoleDto: UpdateRoleDto
  ) {
    return this.roleService.updateRole(
      id,
      updateRoleDto.name,
      updateRoleDto.code,
      updateRoleDto.description,
      updateRoleDto.type
    );
  }

  @Delete(':id')
  @Roles('系统管理员')
  @Permissions('system:role:delete')
  @ApiOperation({ summary: '删除角色' })
  @ApiResponse({ status: 200, description: '角色删除成功' })
  async deleteRole(@Param('id') id: string) {
    return this.roleService.deleteRole(id);
  }

  @Post(':id/permissions')
  @Roles('系统管理员')
  @Permissions('system:role:assignPermissions')
  @ApiOperation({ summary: '分配权限给角色' })
  @ApiResponse({ status: 200, description: '权限分配成功' })
  async assignPermissionsToRole(
    @Param('id') roleId: string,
    @Body() assignPermissionsDto: AssignPermissionsDto
  ) {
    return this.roleService.assignPermissionsToRole(
      roleId,
      assignPermissionsDto.permissionIds
    );
  }

  @Post('users/:userId/roles')
  @Roles('系统管理员')
  @Permissions('system:user:assignRoles')
  @ApiOperation({ summary: '分配角色给用户' })
  @ApiResponse({ status: 200, description: '角色分配成功' })
  async assignRolesToUser(
    @Param('userId') userId: string,
    @Body() assignRolesDto: AssignRolesDto,
  ) {
    return this.roleService.assignRolesToUser(userId, assignRolesDto.roleIds);
  }

  @Get()
  @Public()
  @ApiOperation({ summary: '获取所有角色' })
  @ApiResponse({ status: 200, description: '获取角色列表成功' })
  async getRoles() {
    return this.roleService.getRoles();
  }

  @Get(':id')
  @Roles('系统管理员')
  @Permissions('system:role:read')
  @ApiOperation({ summary: '获取角色详情' })
  @ApiResponse({ status: 200, description: '获取角色详情成功' })
  async getRoleById(@Param('id') id: string) {
    return this.roleService.getRoleById(id);
  }
} 